from lxml import etree
import pymysql
import mysql
from concurrent.futures import ThreadPoolExecutor
import requests
import time

from 兼职吗.垃圾堆 import job_title

def sheep():
    time.sleep(1)
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.44'
}
# job_title()
def company(i):
    # print(job_title())
    # for i in job_title():
    #     i: [0]

    # print(i)
    try:
        reps = requests.get(url=i, headers=headers)
        company_html = reps.text
        tree = etree.HTML(company_html)
        # print(company_html)

        sheep()
        ren_su=tree.xpath('/html/body/section/article/div/div[1]/div[2]/div[2]/ul[1]/li[1]/span[2]/text()')
        di_dian=tree.xpath('/html/body/section/article/div/div[1]/div[2]/div[2]/ul[1]/li[2]/span[2]/text()')
        chang_duan=tree.xpath('/html/body/section/article/div/div[1]/div[2]/div[2]/ul[2]/li[1]/span[2]/text()')
        shi_jian=tree.xpath('/html/body/section/article/div/div[1]/div[2]/div[2]/ul[2]/li[2]/span[2]/text()')
        shi_duan=tree.xpath('/html/body/section/article/div/div[1]/div[2]/div[2]/ul[2]/li[3]/span[2]/text()')
        jie_shuan=tree.xpath('/html/body/section/article/div/div[1]/div[2]/div[2]/ul[3]/li[1]/span[2]/text()')
        gong_zi=tree.xpath('/html/body/section/article/div/div[1]/div[2]/div[2]/ul[3]/li[2]/span[2]/text()')
        xiang_qing=tree.xpath('//*[@id="job_detail"]/text()')
        gongsi_name=tree.xpath('/html/body/section/article/div/div[1]/div[3]/div[2]/div[1]/div[2]/a/text()')
        gongsi_info=tree.xpath('/html/body/section/article/div/div[1]/div[3]/div[2]/div[1]/div[2]/p[1]/text()')
        gongsi_location=tree.xpath('/html/body/section/article/div/div[1]/div[3]/div[2]/div[1]/div[2]/p[2]/text()')
            # print(gong_zi)
# company()
        for data in zip(ren_su,di_dian,chang_duan,shi_jian,shi_duan,jie_shuan,gong_zi,xiang_qing,gongsi_name,gongsi_info,gongsi_location):
            tianjiasuju(a=data[0],b=data[1],c=data[2],d=data[3],e=data[4],f=data[5],g=data[6],h=data[7],i=data[8],j=data[9],k=data[10])
    except:  # 有的西八东西是错的，鱼鱼快动啊，跳过那个
        print("shake it !")
        print("鱼鱼快动啊")
def tianjiasuju(a,b,c,d,e,f,g,h,i,j,k):  # 添加数据库--------
    sql="""
    insert into job_company_info(rensu,location,long_short,work_day,work_hour,settle,salary,job_information,company_name,company_info,company_location)
    values
    ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')
    """% (a,b,c,d,e,f,g,h,i,j,k)
    mysql.gengxinbiao(sql)

def multi_thread():
    with ThreadPoolExecutor(4) as th:
        for i in job_title():
            th.submit(company,i[0])

if __name__ == '__main__':
    multi_thread()
    print("finish")